package com.etc.my.controller;


import com.etc.my.dto.MyMessage;
import com.etc.my.dto.PageDto;
import com.etc.my.dto.PushDto;
import com.etc.my.entity.Push;
import com.etc.my.entity.User;
import com.etc.my.service.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class PushController {


    @Autowired
   private PushService pushs;
    public PushService getPushs() {
        return pushs;
    }
    public void setPushs(PushService pushs) {
        this.pushs = pushs;
    }

    /**
     * 得到用户想要的车辆的控制器
     * @param session
     * @return
     */
    @RequestMapping("/getPushListByUserId")
    @ResponseBody
    public MyMessage getPushListByUserId(HttpSession session,Integer page,Integer size){
        MyMessage message = new MyMessage();
        //获取登录的用户
  User user = (User) session.getAttribute("loginuser");

        if(user!=null){
            //判断是否有取到值
            if(page==null||page<=0){
                page=1;
            }

            if(size==null||size<=0){
                size=3;
            }
            //获取总条数
            Integer allCount = pushs.getCountPushByUserId(user.getUser_id()).getAllCount();
            if(allCount>=0&&allCount!=null){
                Integer maxPage = (allCount%size==0)?(allCount/size):(allCount/size+1);
                //页数条数上限容错
                page=page>maxPage?maxPage:page;
                size=size>3?3:size;
                //页数条数下限容错
                page=page<=0?1:page;
                size=size<=0?3:size;

                PageDto pd = new PageDto();
                pd.setPage(page);
                pd.setSize(size);

                //调用方法查询
                List<Push> pushList = pushs.getPushListByUserIdOfPage(user.getUser_id(),(page-1)*size,size);
                if(pushList.size()>0&&pushList!=null){
                    message.setFlag(1);
                    pd.setMaxPage(maxPage);
                    pd.setAllCount(allCount);
                    pd.setOlist(pushList);
                    message.setData(pd);
                }else{
                    message.setFlag(0);
                    message.setInfo("查无此纪录");
                }
            }else{
                message.setFlag(0);
                message.setInfo("查无此纪录");
            }

        }else{
            message.setFlag(0);
            message.setInfo("您尚未登录请先登录!");
        }

        return message;
    }

    @RequestMapping("/addWantCar")
    @ResponseBody
    public void addWantCar(PushDto pushDto, HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        //处理车辆品牌车型  品牌/车型
        if(pushDto.getPush_brand()!=null&&!pushDto.getPush_brand().equals("")){
            String[] brand = pushDto.getPush_brand().split("/");
            pushDto.setPush_brand(brand[0]);
            pushDto.setPush_dept(brand[1]);
        }
        if (pushs.addWantCar(pushDto)){
            pw.print("<script>alert('添加成功');location.href='index.jsp'</script>");
        }else {
            pw.print("<script>alert('添加失败');history.go(-1)</script>");
        }
        pw.flush();
        pw.close();
    }

    @RequestMapping("/removefavorite")
    public void removefavorite(Integer id,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(pushs.deletePush(id)){
            out.print("<script>alert('删除成功！');location.href='myfavorite.jsp'</script>");
        }else {
            out.print("<script>alert('删除失败！');location.href='myfavorite.jsp'</script>");
        }
        out.flush();
        out.close();
    }

}
